﻿package com.view 
{
	import com.Model.vo.WomanVo;
	import com.Model.WomanProxy;
	import com.utils.DebugTrace;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import com.greensock.*; 
	import com.greensock.easing.*;
	
	/**
	 * ...
	 * @author Mr.zheng
	 */
	public class ApplicationMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "applicationMediator_name";
		public static const INITWOMAN:String = "applicationMediator_init_woman";//初始化女人
		
		private static const womanMcX:int = 100;
		private static const womanMcY:int = 50;
		private static const paintMenuMcX:int=20;
		private static const paintMenuMcY:int = 10;
		private static const clothMenuMcX:int=20;
		private static const clothMenuMcY:int = 10;
		
		public static const SCALEXY:Number = 1.05;
		
		public function ApplicationMediator(mediatorName:String = null, viewComponent:Object = null) 
		{
			super(mediatorName, viewComponent);
			
		}
		override public function listNotificationInterests():Array 
		{
			return [ApplicationFacade.OVERSTARTUP]
		}

		override public function handleNotification(notification:INotification):void 
		{
			super.handleNotification(notification);
			var name:String = notification.getName();
			switch(name) {
				case ApplicationFacade.OVERSTARTUP:
					var bg:MovieClip = facade.retrieveMediator(BgMediator.NAME).getViewComponent().bg as MovieClip;
					Stage(viewComponent.stage).addChild(bg);
					showFrame1();
					addMusicMc();
					//addChildWoman();
					//addPaintMenu();
					//addClothMenu();
					break;
			}
		}
		private function showFrame1():void {
			var frame1:MovieClip = new Frame1();
			frame1.name = "frame1";
			Stage(viewComponent.stage).addChild(frame1);
			frame1.play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
			addBtnEvent(frame1.play_btn);
			frame1.moreGame_btn.addEventListener(MouseEvent.CLICK, onMoreGameClick);
			addBtnEvent(frame1.moreGame_btn);
		}
		private function addMusicMc():void {
			var musicMc:MovieClip = new MusicMc();
			musicMc.x = 670;
			musicMc.y = 20;
			musicMc.scaleX = .5;
			musicMc.scaleY = .5;
			musicMc.isPlay = true;
			Stage(viewComponent.stage).addChild(musicMc);
			musicMc.addEventListener(MouseEvent.CLICK, onMusicClick);
			
			var bgMusic:MovieClip = new BgMp3();
			var btnMusic:MovieClip = new BtnMusic();
			btnMusic.name = "btnMusic";
			viewComponent.btnMusic = btnMusic;
			viewComponent.bgMusic = bgMusic;
			Stage(viewComponent.stage).addChild(btnMusic);
			Stage(viewComponent.stage).addChild(bgMusic);
		}
		
		private function onMusicClick(e:MouseEvent):void 
		{
			var mc:MovieClip = e.currentTarget as MovieClip;
			var isPlay:Boolean = mc.isPlay as Boolean;
			if (isPlay) {
				mc.isPlay = false;
				mc.stop();
				viewComponent.bgMusic.stop();
			}else {
				mc.isPlay = true;
				mc.play();
				viewComponent.bgMusic.play();
			}
		}
		private function delFrame1():void {
			var frame1:MovieClip = Stage(viewComponent.stage).getChildByName("frame1") as MovieClip;
			frame1.play_btn.removeEventListener(MouseEvent.CLICK, onPlayClick);
			delBtnEvent(frame1.play_btn);
			frame1.moreGame_btn.removeEventListener(MouseEvent.CLICK, onMoreGameClick);
			delBtnEvent(frame1.moreGame_btn);
			Stage(viewComponent.stage).removeChild(frame1);
		}
		private function addChildWoman():void {
			var womanMc:MovieClip = facade.retrieveMediator(WomanMediator.NAME).getViewComponent().womanMc as MovieClip;
			womanMc.x = womanMcX;
			womanMc.y = womanMcY;
			womanMc.scaleX = 2;
			womanMc.scaleY = 2;
			
			Stage(viewComponent.stage).addChild(womanMc);
		}
		private function addPaintMenu():void {
			var paintMenuMc:MovieClip = facade.retrieveMediator(PaintMenuMediator.NAME).getViewComponent().paintMenuMc as MovieClip;
			paintMenuMc.x = paintMenuMcX; 
			paintMenuMc.y = paintMenuMcY;
			Stage(viewComponent.stage).addChild(paintMenuMc);
		}
		private function showFrame2():void {
			var next_btn:SimpleButton = new NextBtn();
			next_btn.name = "next_btn";
			next_btn.x = 540;
			next_btn.y = 400;
			Stage(viewComponent.stage).addChild(next_btn);
			next_btn.addEventListener(MouseEvent.CLICK, onNextClick);
			addBtnEvent(next_btn);
			
			sendNotification(MouseMediator.SHOW_MOUSE);
		}
		private function delFrame2():void {
			var next_btn:SimpleButton = Stage(viewComponent.stage).getChildByName("next_btn") as SimpleButton;
			next_btn.removeEventListener(MouseEvent.CLICK, onNextClick);
			delBtnEvent(next_btn);
			Stage(viewComponent.stage).removeChild(next_btn);
			
			var paintMenuMc:MovieClip = facade.retrieveMediator(PaintMenuMediator.NAME).getViewComponent().paintMenuMc as MovieClip;
			Stage(viewComponent.stage).removeChild(paintMenuMc);
			
			sendNotification(MouseMediator.HIDE_MOUSE);
		}
		private function addClothMenu():void {
			var clothMenuMc:MovieClip = facade.retrieveMediator(ClothMenuMediator.NAME).getViewComponent().clothMenuMc as MovieClip;
			clothMenuMc.x = clothMenuMcX;
			clothMenuMc.y = clothMenuMcY-200;
			Stage(viewComponent.stage).addChild(clothMenuMc);
			TweenMax.to(clothMenuMc, 1, { bezier:[ { x:clothMenuMcX, y:clothMenuMcY} ], ease:Expo.easeOut } );
		}
		private function delMenu(mc:MovieClip):void {
			//var clothMenuMc:MovieClip = facade.retrieveMediator(ClothMenuMediator.NAME).getViewComponent().clothMenuMc as MovieClip;
			TweenMax.to(mc, 1, { bezier:[ {x:mc.x-360 } ], ease:Expo.easeOut,onComplete:onMoveComplete,onCompleteParams:[mc] } );
			
		}
		private function onMoveComplete(mc:MovieClip):void {
			mc.parent.removeChild(mc);
		}
		private function moveWoman():void {
			var womanMc:MovieClip = facade.retrieveMediator(WomanMediator.NAME).getViewComponent().womanMc as MovieClip;
			TweenMax.to(womanMc, 1, { bezier:[ { x:350, y:10, scaleX:.4, scaleY:.4 } ], ease:Expo.easeOut } );
		}
		/**
		 * 显示第三帧
		 */
		private function showFrame3():void {
			//显示第三帧
			moveWoman();
			addClothMenu();
			var frame3Mc:MovieClip = new Frame3Mc();
			frame3Mc.x = 540;
			frame3Mc.y = 10;
			frame3Mc.name = "frame3Mc";
			Stage(viewComponent.stage).addChild(frame3Mc);
			frame3Mc.back_btn.addEventListener(MouseEvent.CLICK, onBackClick);
			addBtnEvent(frame3Mc.back_btn);
			
			frame3Mc.done_btn.addEventListener(MouseEvent.CLICK, onDoneClick);
			addBtnEvent(frame3Mc.done_btn);
			
			frame3Mc.bg_btn.addEventListener(MouseEvent.CLICK, obBgClick);
			addBtnEvent(frame3Mc.bg_btn);
		}
		private function delFrame3():void {
			var frame3Mc:MovieClip = Stage(viewComponent.stage).getChildByName("frame3Mc") as MovieClip;
			frame3Mc.back_btn.removeEventListener(MouseEvent.CLICK, onBackClick);
			delBtnEvent(frame3Mc.back_btn);
			
			frame3Mc.done_btn.removeEventListener(MouseEvent.CLICK, onDoneClick);
			delBtnEvent(frame3Mc.done_btn);
			
			frame3Mc.bg_btn.removeEventListener(MouseEvent.CLICK, obBgClick);
			delBtnEvent(frame3Mc.bg_btn);
			
			Stage(viewComponent.stage).removeChild(frame3Mc);
			
			var clothMenuMc:MovieClip = facade.retrieveMediator(ClothMenuMediator.NAME).getViewComponent().clothMenuMc as MovieClip;
			delMenu(clothMenuMc);
		}
		/**
		 * 显示第四帧
		 */
		private function showFrame4():void {
			var womanMc:MovieClip = facade.retrieveMediator(WomanMediator.NAME).getViewComponent().womanMc as MovieClip;
			TweenMax.to(womanMc, 1, { bezier:[ { x:200} ], ease:Expo.easeOut } );
			
			var frame4Mc:MovieClip = new Frame4Mc();
			frame4Mc.name = "frame4Mc";
			frame4Mc.x = 540;
			frame4Mc.y = 327;
			frame4Mc.back_btn.addEventListener(MouseEvent.CLICK, onBack2Click);
			addBtnEvent(frame4Mc.back_btn);
			
			frame4Mc.menu_btn.addEventListener(MouseEvent.CLICK, onMenuClick);
			addBtnEvent(frame4Mc.menu_btn);
			
			frame4Mc.moreGame_btn.addEventListener(MouseEvent.CLICK, onMoreGameClick);
			addBtnEvent(frame4Mc.moreGame_btn);
			
			Stage(viewComponent.stage).addChild(frame4Mc);
		}
		private function delFrame4():void {
			var frame4Mc:MovieClip = Stage(viewComponent.stage).getChildByName("frame4Mc") as MovieClip;
			frame4Mc.back_btn.removeEventListener(MouseEvent.CLICK, onBack2Click);
			delBtnEvent(frame4Mc.back_btn);
			
			frame4Mc.menu_btn.removeEventListener(MouseEvent.CLICK, onMenuClick);
			delBtnEvent(frame4Mc.menu_btn);
			
			frame4Mc.moreGame_btn.removeEventListener(MouseEvent.CLICK, onMoreGameClick);
			delBtnEvent(frame4Mc.moreGame_btn);
			Stage(viewComponent.stage).removeChild(frame4Mc);
		}
		private function onPlayClick(e:MouseEvent):void 
		{
			delFrame1();
			sendNotification(INITWOMAN);
			addChildWoman();
			addPaintMenu();
			showFrame2();
		}
		
		private function onNextClick(e:MouseEvent):void 
		{
			//删除第二帧
			//delMenu(paintMenuMc);
			delFrame2();
			showFrame3();
		}
		private function onMoreGameClick(e:MouseEvent):void 
		{
			DebugTrace.dtrace("code info ApplicationMediator.as:moreGame_btn is clicked!");
		}
		private function obBgClick(e:MouseEvent):void 
		{
			sendNotification(BgMediator.BGCHANGED);
		}
		
		private function onDoneClick(e:MouseEvent):void 
		{
			//删除第三帧
			delFrame3();
			showFrame4();
		}
		
		/**
		 * 回到第二帧
		 * @param	e
		 */
		private function onBackClick(e:MouseEvent):void 
		{
			delFrame3();
			var womanMc:MovieClip = facade.retrieveMediator(WomanMediator.NAME).getViewComponent().womanMc as MovieClip;
			TweenMax.to(womanMc, 1, { bezier:[ { x:womanMcX, y:womanMcY, scaleX:2, scaleY:2 } ], ease:Expo.easeOut } );
			addPaintMenu();
			showFrame2();
		}
		private function onBack2Click(e:MouseEvent):void 
		{
			delFrame4();
			moveWoman();
			addClothMenu();
			showFrame3();
		}
		private function onMenuClick(e:MouseEvent):void 
		{
			sendNotification(BgMediator.INITBG);//初始化bg
			delFrame4();
			var womanMc:MovieClip = facade.retrieveMediator(WomanMediator.NAME).getViewComponent().womanMc as MovieClip;
			var womanVo:WomanVo = facade.retrieveProxy(WomanProxy.NAME).getData() as WomanVo;
			facade.retrieveProxy(WomanProxy.NAME).setData(new WomanVo());
			Stage(viewComponent.stage).removeChild(womanMc);
			showFrame1();
		}
		private function addBtnEvent(mc:*):void {
			mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			mc.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			mc.addEventListener(MouseEvent.CLICK, onClick);
		}
		private function delBtnEvent(mc:*):void {
			mc.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
			mc.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
			mc.removeEventListener(MouseEvent.CLICK, onClick);
		}
		private function onRollOver(e:MouseEvent ):void {
			viewComponent.btnMusic.gotoAndPlay(2);
			DisplayObject(e.currentTarget).scaleX = SCALEXY;
			DisplayObject(e.currentTarget).scaleY = SCALEXY;
		}
		private function onRollOut(e:MouseEvent):void {
			DisplayObject(e.currentTarget).scaleX = 1;
			DisplayObject(e.currentTarget).scaleY = 1;
		}
		private function onClick(e:MouseEvent):void {
			viewComponent.btnMusic.gotoAndPlay(4);
		}
	}

}